﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Supermarket.Dal;

namespace Supermarket.Bll
{
    public static class InquireManager
    {
        public static List<ViewAllProductsInStock> GetAllProducts(string sortExpression, int categoryId)
        {
            List<ViewAllProductsInStock> products = InquireDB.GetAllProducts(categoryId);
            products.Sort(new AllProductsComparer(sortExpression));
            return products;
        }

        private class AllProductsComparer : IComparer<ViewAllProductsInStock>
        {
            private string _sortColumn;
            private bool _reverse;
            
            public AllProductsComparer(string sortExpression)
            {
                if (sortExpression.Length == 0)
                {
                    sortExpression = "ProductId desc";
                }
                _reverse = sortExpression.ToLowerInvariant().EndsWith(" desc");
                if (_reverse)
                {
                    _sortColumn = sortExpression.Substring(0, sortExpression.Length - 5);
                }
                else
                {
                    _sortColumn = sortExpression;
                }
            }

       
            public int Compare(ViewAllProductsInStock a, ViewAllProductsInStock b)
            {
                int retVal = 0;
                switch (_sortColumn.ToLower())
                {
                    case "productid":
                        retVal = a.ProductId.CompareTo(b.ProductId);
                        break;
                    case "productname":
                        retVal = a.ProductName.CompareTo(b.ProductName);
                        break;
                    case "categoryname":
                        retVal = a.CategoryName.CompareTo(b.CategoryName);
                        break;
                    case "instock":
                        retVal = a.InStock.CompareTo(b.InStock);
                        break;
                    case "productprice":
                        retVal = a.ProductPrice.CompareTo(b.ProductPrice);
                        break;
                    case "totalitemprice":
                        if (a.TotalItemPrice < b.TotalItemPrice)
                        {
                            retVal = -1;
                        }
                        else if (a.TotalItemPrice == b.TotalItemPrice)
                        {
                            retVal = 0;
                        }
                        else if(a.TotalItemPrice > b.TotalItemPrice)
                        {
                            retVal = 1;
                        }
                        break;
                }

                int _reverseInt = 1;
                if ((_reverse))
                {
                    _reverseInt = -1;
                }
                return (retVal * _reverseInt);
            }
        }

        //View All Sales
        public static List<ViewSale> GetAllSales(DateTime fromDate, DateTime toDate, string sortExpression)
        {
            List<ViewSale> sales = InquireDB.GetAllSales(fromDate, toDate);
            sales.Sort(new AllSalesComparer(sortExpression));
            return sales;
        }

        private class AllSalesComparer : IComparer<ViewSale>
        {
            private string _sortColumn;
            private bool _reverse;

            public AllSalesComparer(string sortExpression)
            {
                if (sortExpression.Length == 0)
                {
                    sortExpression = "ProductName desc";
                }
                _reverse = sortExpression.ToLowerInvariant().EndsWith(" desc");
                if (_reverse)
                {
                    _sortColumn = sortExpression.Substring(0, sortExpression.Length - 5);
                }
                else
                {
                    _sortColumn = sortExpression;
                }
            }


            public int Compare(ViewSale a, ViewSale b)
            {
                int retVal = 0;
                switch (_sortColumn.ToLower())
                {
                    case "productname":
                        retVal = a.ProductName.CompareTo(b.ProductName);
                        break;
                    case "quantity":
                        retVal = a.Quantity.CompareTo(b.Quantity);
                        break;
                    case "saleon":
                        retVal = a.SaleOn.CompareTo(b.SaleOn);
                        break;
                    case "totalitemprice":
                        if (a.TotalItemPrice < b.TotalItemPrice)
                        {
                            retVal = -1;
                        }
                        else if (a.TotalItemPrice == b.TotalItemPrice)
                        {
                            retVal = 0;
                        }
                        else if (a.TotalItemPrice > b.TotalItemPrice)
                        {
                            retVal = 1;
                        }
                        break;
                }

                int _reverseInt = 1;
                if ((_reverse))
                {
                    _reverseInt = -1;
                }
                return (retVal * _reverseInt);
            }
        }

        //ProductsInStockOnDate
        public static double GetSaleQuantity(int productId, DateTime fromDate)
        {
            double quantity = InquireDB.GetSaleQuantity(productId, fromDate);
            return quantity;
        }

        public static double GetSupplyQuantity(int productId, DateTime fromDate)
        {
            double quantity = InquireDB.GetSupplyQuantity(productId, fromDate);
            return quantity;
        }
    }
}
